package com.neusoft.neuiotms.dcom.converter;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.MaintainRuleDTO;
import com.neusoft.neuiotms.dcom.model.MaintainRule;
import com.neusoft.neuiotms.dcom.repository.MaintainRuleRepository;

@Component
public class MaintainRuleConverter extends AbstractConverter<MaintainRule, MaintainRuleDTO> {

    @Autowired
    private MaintainRuleRepository maintainRuleRepository;

    @Autowired
    private BaseIdGenerator maintainRuleCodeGenerator;

    @Override
    public MaintainRule toModel(final MaintainRuleDTO dto) {
        MaintainRule model;
        if (dto.isNew()) {
            model = new MaintainRule();
            model.setCreateTime(new Date());
        } else {
            model = this.maintainRuleRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setName(dto.getName());
        model.setCode(
                StringUtils.isEmpty(dto.getCode()) ? this.maintainRuleCodeGenerator.generateCode() : dto.getCode());

        model.setModelCode(dto.getModelCode());
        model.setModelName(dto.getModelName());
        model.setSuspendService(dto.isSuspendService());
        model.setQuantifyingTime(dto.getQuantifyingTime());
        model.setDescription(dto.getDescription());

        model.setStatus(dto.getStatus());

        model.setUserId(dto.getUserId());
        model.setUserName(dto.getUserName());

        model.setSource(dto.getSource());

        return model;
    }

    @Override
    public MaintainRuleDTO toDTO(final MaintainRule model, final boolean forListView) {
        final MaintainRuleDTO dto = new MaintainRuleDTO();
        dto.setId(model.getId());

        dto.setName(model.getName());
        dto.setCode(model.getCode());

        dto.setModelCode(model.getModelCode());
        dto.setModelName(model.getModelName());
        dto.setSuspendService(model.isSuspendService());
        dto.setQuantifyingTime(model.getQuantifyingTime());
        dto.setDescription(model.getDescription());

        dto.setStatus(model.getStatus());
        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        dto.setSource(model.getSource());
        return dto;
    }

}
